package cn.kgc.tangcco.utils.token;

import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTCreator;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.exceptions.AlgorithmMismatchException;
import com.auth0.jwt.exceptions.InvalidClaimException;
import com.auth0.jwt.exceptions.SignatureVerificationException;
import com.auth0.jwt.exceptions.TokenExpiredException;
import com.auth0.jwt.interfaces.DecodedJWT;
import io.jsonwebtoken.*;

import java.util.Date;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

/**
 * @author 李昊哲
 * @Description jwt工具类
 * @Description jwt工具类
 * @date 2020/12/10 下午2:20
 */
public abstract class BaseJwtUtil {
    /**
     * 生成token
     *
     * @param key        密钥
     * @param param      playload
     * @param expiryDate token有效期 long类型
     * @return token
     */
    public static String jjwtEncode(String key, Map<String, Object> param, long expiryDate) {
        Date date = new Date();
        Date iat = date;
        // 过期时间
        date.setTime(date.getTime() + expiryDate);
        return Jwts.builder().setExpiration(date).setClaims(param).signWith(SignatureAlgorithm.HS256, key).compact();
    }

    /**
     * 解析token
     *
     * @param token token
     * @param key   密钥
     * @return playload
     */
    public static Map<String, Object> jjwtDecode(String token, String key) {
        try {
            return Jwts.parserBuilder().setSigningKey(key).build().parseClaimsJws(token).getBody();
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 生成token
     *
     * @param key        密钥
     * @param param      playload
     * @param expiryDate token有效期 long类型
     * @return token
     */
    public static String jwtEncode(String key, Map<String, String> param, long expiryDate) {
        // 过期时间
        JWTCreator.Builder builder = JWT.create();
        // payload
        Set<Map.Entry<String, String>> entries = param.entrySet();
        Iterator<Map.Entry<String, String>> iterator = entries.iterator();
        while (iterator.hasNext()){
            Map.Entry<String, String> entry = iterator.next();
            builder.withClaim(entry.getKey(),entry.getValue());
        }
        return builder.withIssuedAt(new Date()).withExpiresAt(new Date(System.currentTimeMillis()+ expiryDate)).sign(Algorithm.HMAC256(key));
    }

    /**
     * 解析token
     *
     * @param token token
     * @param key   密钥
     * @return playload
     */
    public static DecodedJWT jwtDecode(String token, String key) {
        try {
            return JWT.require(Algorithm.HMAC256(key)).build().verify(token);
        }catch (SignatureVerificationException e){
            System.out.println("签名不一致");
            return null;
        }catch (AlgorithmMismatchException e){
            System.out.println("算法不匹配");
            return null;
        }catch (TokenExpiredException e){
            System.out.println("token过期失效");
            return null;
        }catch (InvalidClaimException e){
            System.out.println("payload失效");
            return null;
        }
    }

}
